<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="学年" label-width="40px">
        <el-input
          v-model="queryParams['tbAcademicYear.academicYearName']"
          placeholder="请输入学年"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学期">
        <el-input
          v-model="queryParams['tbSemester.semesterName']"
          placeholder="请输入学期ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="科目">
        <el-input
          v-model="queryParams['tbSubject.subjectName']"
          placeholder="请输入科目"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="教研组">
        <el-input
          v-model="
            queryParams['tbTeachingResearchSection.teachingResearchSectionName']
          "
          placeholder="请输入教研组"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="考试方式">
        <el-input
          v-model="
            queryParams['tbAssessmentMethod.assessmentMethodName']"
          placeholder="请输入考试方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

<!--      <el-form-item label="考核方式" prop="assessmentMethodId">
        <el-select
          v-model="queryParams.assessmentMethodId"
          placeholder="请选择考核方式ID"
          clearable
        >
          <el-option
            v-for="dict in dict.type.assessment_method_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>-->

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['exam_syllabus:syllabus:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['exam_syllabus:syllabus:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['exam_syllabus:syllabus:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['exam_syllabus:syllabus:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="syllabusList" :header-cell-style="{ background: '#87ceeb' }" border :row-class-name="tableRowClassName"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="序号"
        align="center"
        type="index"
        :index="indexMethod"
      ></el-table-column>
      <el-table-column
        label="学年"
        align="center"
        prop="tbAcademicYear.academicYearName"
      />
      <el-table-column
        label="学期"
        align="center"
        prop="tbSemester.semesterName"
      />
      <el-table-column
        label="科目"
        align="center"
        prop="tbSubject.subjectName"
      />
      <el-table-column
        label="教研组"
        align="center"
        prop="tbTeachingResearchSection.teachingResearchSectionName"
      />
      <el-table-column
        label="考试方式"
        align="center"
        prop="tbAssessmentMethod.assessmentMethodName"
      />
<!--      <el-table-column
        label="考核方式"
        align="center"
        prop="assessmentMethodId"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.assessment_method_type"
            :value="scope.row.assessmentMethodId"
          />
        </template>
      </el-table-column>-->
      <el-table-column
        label="出题教师"
        align="center"
        prop="tbTeacher.teacherName"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button type="info" @click="handleRouter(scope.row)"
            >详情</el-button
          >
          <el-button
            type="primary"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['exam_syllabus:syllabus:edit']"
            >修改</el-button
          >
          <el-button
            type="danger"
            @click="handleDelete(scope.row)"
            v-hasPermi="['exam_syllabus:syllabus:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改考试大纲设置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
        label-position="“left”"
      >
        <el-form-item label="学年" prop="academicYearId">
          <el-select
            v-model="form.academicYearId"
            placeholder="请输入学年"
            clearable
          >
            <!--label这里面的东西才是我们要拿的-->
            <el-option
              v-for="item in academicYearList"
              :key="item.academicYearId"
              :label="item.academicYearName"
              :value="item.academicYearId"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="学期" prop="semesterId">
          <el-select
            v-model="form.semesterId"
            placeholder="请输入学期"
            clearable
          >
            <!--label这里面的东西才是我们要拿的-->
            <el-option
              v-for="item in semesterList"
              :key="item.semesterId"
              :label="item.semesterName"
              :value="item.semesterId"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="科目" prop="subjectId">
          <el-select
            v-model="form.subjectId"
            placeholder="请输入科目"
            clearable
          >
            <!--label这里面的东西才是我们要拿的-->
            <el-option
              v-for="item in subjectList"
              :key="item.subjectId"
              :label="item.subjectName"
              :value="item.subjectId"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="教研组" prop="teachingResearchSectionId">
          <el-select
            v-model="form.teachingResearchSectionId"
            placeholder="请输入教研组"
            clearable
          >
            <!--label这里面的东西才是我们要拿的-->
            <el-option
              v-for="item in teachingResearchSectionList"
              :key="item.teachingResearchSectionId"
              :label="item.teachingResearchSectionName"
              :value="item.teachingResearchSectionId"
            ></el-option>
          </el-select>
        </el-form-item>

<!--        <el-form-item label="考核方式" prop="assessmentMethodId">
          <el-select
            v-model="form.assessmentMethodId"
            placeholder="请选择考核方式ID"
          >
            <el-option
              v-for="dict in dict.type.assessment_method_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>-->
        <el-form-item label="考试方式" prop="assessmentMethodId">
          <el-select
            v-model="form.assessmentMethodId"
            placeholder="请选择考核方式"
            clearable
          >
            <!--label这里面的东西才是我们要拿的-->
            <el-option
              v-for="item in assessmentMethodIdList"
              :key="item.assessmentMethodId"
              :label="item.assessmentMethodName"
              :value="item.assessmentMethodId"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="教师" prop="teacherId">
          <el-select
            v-model="form.teacherId"
            placeholder="请输入教师"
            clearable
          >
            <!--label这里面的东西才是我们要拿的-->
            <el-option
              v-for="item in teacherList"
              :key="item.teacherId"
              :label="item.teacherName"
              :value="item.teacherId"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listSyllabus, getSyllabus, delSyllabus, addSyllabus, updateSyllabus} from "@/api/exam_syllabus/syllabus";
import { listYear } from "@/api/basice_info/year";
import { listSemester } from "@/api/basice_info/semester";
import { listSection } from "@/api/basice_info/section";
import { listSubject } from "@/api/basice_info/subject";
import { listTeacher } from "@/api/basice_info/teacher";

import {listMethod} from "@/api/basice_info/method";

export default {
  name: "Syllabus",
  // dicts: ["assessment_method_type"],
  data() {
    return {

      // 考试方式
      assessmentMethodIdList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 学年
      academicYearList: [],
      // 学期
      semesterList: [],
      // 科目
      subjectList: [],
      // 教研组
      teachingResearchSectionList: [],
      // 教师
      teacherList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考试大纲设置表格数据
      syllabusList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        academicYearId: null,
        semesterId: null,
        subjectId: null,
        teachingResearchSectionId: null,
        assessmentMethodId: null,
        "tbAcademicYear.academicYearName": null,
        "tbSemester.semesterName": null,
        "tbSubject.subjectName": null,
        "tbTeachingResearchSection.teachingResearchSectionName": null,
        "tbAssessmentMethod.assessmentMethodName": null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        academicYearId: [
          { required: true, message: "学年ID不能为空", trigger: "blur" },
        ],
        semesterId: [
          { required: true, message: "学期ID不能为空", trigger: "blur" },
        ],
        subjectId: [
          { required: true, message: "科目ID不能为空", trigger: "blur" },
        ],
        teachingResearchSectionId: [
          { required: true, message: "教研组ID不能为空", trigger: "blur" },
        ],
        assessmentMethodId: [
          { required: true, message: "考核方式ID不能为空", trigger: "change" },
        ],
        teacherId: [
          { required: true, message: "教师ID不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    // 学年
    this.getacademicYearList();
    // 教师
    this.getteacherList();
    // 科目
    this.getsubjectList();
    // 学期
    this.getsemesterList();
    // 教研组
    this.getteachingResearchSectionList();
    // 考试方式
    this.getMethodIdList();
  },
  methods: {
    // 序号
    indexMethod(index) {
      return (
        (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1
      );
    },
    // 表格颜色
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 === 0) {
        return "success-row";
      } else {
        return "warning-row";
      }
    },
    /** 查询考试大纲设置列表 */
    getList() {
      this.loading = true;
      listSyllabus(this.queryParams).then((response) => {
        this.syllabusList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 考试方式
    getMethodIdList() {
      listMethod().then((response) => {
        this.assessmentMethodIdList = response.rows;
        // console.log(this.assessmentMethodIdList);
      });
    },
    // 学年
    getacademicYearList() {
      listYear().then((response) => {
        this.academicYearList = response.rows;
      });
    },
    // 学期
    getsemesterList() {
      listSemester().then((response) => {
        this.semesterList = response.rows;
      });
    },
    // 科目
    getsubjectList() {
      listSubject().then((response) => {
        this.subjectList = response.rows;
      });
    },
    // 教研组
    getteachingResearchSectionList() {
      listSection().then((response) => {
        this.teachingResearchSectionList = response.rows;
      });
    },
    // 教师
    getteacherList() {
      listTeacher().then((response) => {
        this.teacherList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        examSyllabusId: null,
        academicYearId: null,
        semesterId: null,
        subjectId: null,
        teachingResearchSectionId: null,
        assessmentMethodId: null,
        teacherId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.examSyllabusId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加考试大纲设置";
    },
    // 跳转页面
    handleRouter(row) {
      this.$router.push({
        path: "/exam_syllabus/details",
        query: { examSyllabusId: row.examSyllabusId },
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const examSyllabusId = row.examSyllabusId || this.ids;
      getSyllabus(examSyllabusId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改考试大纲设置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.examSyllabusId != null) {
            updateSyllabus(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSyllabus(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const examSyllabusIds = row.examSyllabusId || this.ids;
      this.$modal
        .confirm(
          '是否确认删除考试大纲设置编号为"' + examSyllabusIds + '"的数据项？'
        )
        .then(function () {
          return delSyllabus(examSyllabusIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "exam_syllabus/syllabus/export",
        {
          ...this.queryParams,
        },
        `syllabus_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style>
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}
</style>
